package com.xinghou.platform.server.mq;

import java.io.Closeable;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeoutException;
import java.util.Properties;

import com.alibaba.rocketmq.client.consumer.MQConsumer;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.xinghou.platform.util.StringUtils;
import org.apache.log4j.Logger;

import com.xinghou.platform.util.ConfigUtil;

public class MqMessage implements Closeable {

    private static Logger logger = Logger.getLogger(MqMessage.class);
    //阿里云key
    private String accessKey;
    //阿里云secretkey
    private String secretKey;
    //生产者id
    private String producerId;
    private Producer producer;


    public String TOPIC_CARD = "user_create";
    public String TOPIC_EXPOSURE = "user_create";
    public String TOPIC_INTEREST = "user_create";
    public String TOPIC_STORE = "user_create";
    public String TOPIC_USER_GEO = "user_create";
    public String TOPIC_USER = "user_create";
    public String TOPIC_HXREGISTER = "user_create";
    public String TOPIC_COMMON="user_create";


    public MqMessage() {
        accessKey = ConfigUtil.getProperty("mq_accessKey");
        secretKey = ConfigUtil.getProperty("mq_secretKey");
        producerId = ConfigUtil.getProperty("producerId");
        TOPIC_CARD = ConfigUtil.getProperty("message_topic");
        TOPIC_EXPOSURE = ConfigUtil.getProperty("message_topic");
        TOPIC_INTEREST = ConfigUtil.getProperty("message_topic");
        TOPIC_STORE = ConfigUtil.getProperty("message_topic");
        TOPIC_USER_GEO = ConfigUtil.getProperty("message_topic");
        TOPIC_USER = ConfigUtil.getProperty("message_topic");
        TOPIC_HXREGISTER = ConfigUtil.getProperty("message_topic");
        TOPIC_COMMON=ConfigUtil.getProperty("message_topic");
        try {
            init(producerId, accessKey, secretKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public MqMessage(String producerId, String accessKey, String secretKey) {
        try {
            init(producerId, accessKey, secretKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void init(String producerId, String accessKey, String secretKey) throws IOException, TimeoutException {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.ProducerId, producerId);
        properties.put(PropertyKeyConst.AccessKey, accessKey);
        properties.put(PropertyKeyConst.SecretKey, secretKey);
        producer = ONSFactory.createProducer(properties);
        producer.start();

    }

    public void sendMessageHXRegister(String message) {
        String topic = TOPIC_HXREGISTER;
        Integer bizcode = MqConstant.MQ_BIZ_HXREGISTER;
        sendMessage(topic, bizcode.toString(), message);
    }
    public void sendLoginHistory(String message)
    {
        String topic=TOPIC_COMMON;
        Integer bizcode=MqConstant.MQ_BIZ_HISTORY_CREATE;
        sendMessage(topic,bizcode.toString(),message);
    }



    public void sendMessageStore(String message, boolean update) {
        String topic = TOPIC_STORE;
        Integer bizcode = MqConstant.MQ_BIZ_STORE_UPDATE;
        sendMessage(topic, bizcode.toString(), message, update);
    }

    public void sendMessageUsergeo(String message, boolean update) {
        String topic = TOPIC_USER_GEO;
        Integer bizcode = MqConstant.MQ_BIZ_USER_GEO_UPDATE;
        sendMessage(topic, bizcode.toString(), message, update);
    }

    public void sendMessageCard(String message, boolean update) {
        String topic = TOPIC_CARD;
        Integer bizcode = MqConstant.MQ_BIZ_CARD_UPDATE;
        sendMessage(topic, bizcode.toString(), message, update);
    }


    public void sendMessageInterest(String message, boolean update) {
        String topic = TOPIC_INTEREST;
        Integer bizcode = MqConstant.MQ_BIZ_INTEREST_UPDATE;
        sendMessage(topic, bizcode.toString(), message, update);
    }

    public void sendMessageExposure(String message, boolean update) {
        String topic = TOPIC_EXPOSURE;
        Integer bizcode = MqConstant.MQ_BIZ_EXPOSURE_UPDATE;
        sendMessage(topic, bizcode.toString(), message, update);
    }

    public void sendMessageGPS(String message)
    {
        String topic=TOPIC_COMMON;
        Integer bizcode=MqConstant.MQ_BIZ_UPDATE_GPS;
        sendMessage(topic,bizcode.toString(),message);
    }


    public void sendMessageUser(String message, boolean update) {
        String topic = TOPIC_USER;
        Integer bizcode = MqConstant.MQ_BIZ_USER_UPDATE;
        sendMessage(topic, bizcode.toString(), message, update);
    }
    public void sendMessage(String topic, String key, String message, boolean update) {
        sendMessage(topic, null, key, message, update);
    }

    public void sendMessage(String topic, String key, String message) {
        sendMessage(topic, null, key, message);
    }


    public void sendMessage(String topic, String tag, String key, String message, boolean update) {
        try {
            if (update) {
                message = message + ":U";
            } else {
                message = message + ":S";
            }
            Message msg = new Message(topic, tag, key, message.getBytes());
            if (!StringUtils.isEmpty(key))
                msg.setKey(key);
            com.aliyun.openservices.ons.api.SendResult sendResult = producer.send(msg);
            if (logger.isInfoEnabled()) {
                logger.info("X sendMessage :" + message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送消息失败：" + message, e);
        }
    }

    public void sendMessage(String topic, String tag, String key, String message) {
        try {
            Message msg = new Message(topic, tag, message.getBytes());
            if (!StringUtils.isEmpty(key))
                msg.setKey(key);
            com.aliyun.openservices.ons.api.SendResult sendResult = producer.send(msg);
            if (logger.isInfoEnabled()) {

                logger.info("X sendMessage :" + message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("发送消息失败：" + message, e);
        }
    }


    @Override
    public void close() throws IOException {
        try {
            if (producer != null)
                producer.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
